/************************************************************************************
* YLog.cpp                                                                          *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "YLog.h"
#include <QDateTime>

YLog::LogLevel YLog::c_logLevel = YLog::DEBUG;

void YLog::log(LogLevel level, QString message) {
    QString msg;
    msg.append(QDateTime::currentDateTime().toString());
    switch(level) {
    case DEBUG:
        msg.append(" [DEBUG]   ");
        break;
    case INFO:
        msg.append(" [INFO]    ");
        break;
    case WARNING:
        msg.append(" [WARNING] ");
        break;
    case ERROR:
        msg.append(" [ERROR]   ");
        break;
    case FATAL:
        msg.append(" [FATAL]   ");
        break;
    }
    msg.append(message);
    //Use some file later.
    qDebug() << msg;
}

void YLog::logDebug(QString message) {
    log(DEBUG, message);
}

void YLog::logInfo(QString message) {
    log(INFO, message);
}

void YLog::logWarning(QString message) {
    log(WARNING, message);
}

void YLog::logError(QString message) {
    log(ERROR, message);
}


void YLog::logFatal(QString message) {
    log(ERROR, message);
}

void YLog::logQuery(QString queryId, QSqlQuery *query) {
    QSqlError error = query->lastError();
    if(error.isValid()) {
        QString errorText("Query: %1 failed.");
        logError(errorText.arg(queryId));
        if(c_logLevel == DEBUG) {
            QString errorText("DETAILS: \n\t\tDB_ERROR: %1\n\t\tDRIVER_ERROR: %2"
                              "\n--------------------Q-U-E-R-Y-----------------------------\n"
                              "%3"
                              "\n-----------------E-N-D---Q-U-E-R-Y------------------------\n");
            logDebug(errorText.arg(error.databaseText()).
                     arg(error.driverText()).
                     arg(query->executedQuery()));
        }
    }
    else {
        QString msg("Query %1 succeeded.");
        logInfo(msg.arg(queryId));
    }
}
